from typing import List, Optional
from datetime import datetime

class DataCache:
    """数据缓存管理器"""
    _instance = None
    
    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(DataCache, cls).__new__(cls)
            cls._instance._init_cache()
        return cls._instance
    
    def _init_cache(self):
        """初始化缓存"""
        self.processed_prices: List[float] = []
        self.last_update_time: Optional[datetime] = None
        self.data_info: dict = {
            "total_count": 0,
            "time_range": "",
            "granularity": "",
            "strategy": ""
        }
    
    def update_data(self, prices: List[float], start_time: datetime, end_time: datetime, 
                   granularity: str, strategy: str):
        """更新缓存数据"""
        self.processed_prices = prices
        self.last_update_time = datetime.now()
        self.data_info.update({
            "total_count": len(prices),
            "time_range": f"{start_time.strftime('%Y-%m-%d %H:%M')} 至 "
                         f"{end_time.strftime('%Y-%m-%d %H:%M')}",
            "granularity": granularity,
            "strategy": strategy
        })
    
    def get_data(self) -> List[float]:
        """获取缓存的价格数据"""
        return self.processed_prices
    
    def get_data_info(self) -> dict:
        """获取数据信息"""
        return self.data_info
    
    def clear(self):
        """清空缓存"""
        self._init_cache()

# 全局单例实例
data_cache = DataCache() 